package com.lvmama.pangolin.HBaseQueryPrice;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.lvmama.pangolin.busi.config.ConfigManager;
import com.lvmama.pangolin.common.Constant;
import com.lvmama.pangolin.common.DateUtil;
import com.lvmama.pangolin.common.RowkeyGenerator;
import com.lvmama.pangolin.hbaseutil.HBaseUtils;

@Component
public class HBaseTicketHistoryPrice {
	/**  
	* @Title：HBEntityOperation.java
	* @Author:sangxiaoduo
	* @Description:
	* @Date：2017年3月21日上午11:25:00  
	*/
	@Autowired
	private HBaseUtils hBaseUtil;
	public String generateRowkey(String pro_goo_id, Date create_date, String TASKID){
		return RowkeyGenerator.generateRowkey(pro_goo_id, create_date, TASKID);
	}
	
	public List<Map<String, String>> queryByRange(String productId,String tableName,String family){
		ConfigManager config = ConfigManager.getInstance();
		int scanRange =  Integer.valueOf(config.getParamValue("scan_range", "SCAN_RANGE","0"));
		Date now=new Date();
		DateUtil du=new DateUtil();
		String stop_rowkey =generateRowkey(productId,now,"999999999999");
		List<Map<String, String>> resultList=new ArrayList<Map<String, String>>();
		Date halfYearAgo = du.getDateBeforeHalf(now);
		String start_rowkey=generateRowkey(productId,halfYearAgo,"000000000000");
		if(scanRange==0){
			try {
				resultList=hBaseUtil.scanByRange(tableName, start_rowkey, stop_rowkey, family);
				
			} catch (IOException e) {
				e.printStackTrace();
			}
		}else{
			try {
				resultList=hBaseUtil.scanByRange(tableName, start_rowkey, stop_rowkey, family);
				if(resultList!=null&&resultList.size()<3){
					Date yearAgo=du.getDateBeforeYear(now);
					start_rowkey=generateRowkey(productId,yearAgo,"000000000000");
					resultList=hBaseUtil.scanByRange(tableName, start_rowkey, stop_rowkey, family);
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return resultList;
	}
	public List<Map<String, String>> queryByProductId(String productId,String tableName,String family,Date newGrebTime){
		ConfigManager config = ConfigManager.getInstance();
		int scanRange =  Integer.valueOf(config.getParamValue("product_range", "PRODUCT_RANGE","0"));
		DateUtil du=new DateUtil();
		String stop_rowkey =generateRowkey(productId,newGrebTime,"999999999999");
		List<Map<String, String>> resultList=new ArrayList<Map<String, String>>();
		Date enddate=new Date();
		if(scanRange==0){
			enddate = du.getDateBeforeHalf(newGrebTime);
		}else if(scanRange==1){
			enddate = du.getDateBeforeYear(newGrebTime);
		}else if(scanRange==2){
			enddate = du.getDateBeforeTwoYear(newGrebTime);
		}else if(scanRange==3){
			enddate = du.getDateBeforeThreeYear(newGrebTime);
		}else if(scanRange==4){
			enddate = du.getDateBeforeFourYear(newGrebTime);
		}else if(scanRange==5){
			enddate = du.getDateBeforeFiveYear(newGrebTime);
		}
		String start_rowkey=generateRowkey(productId,enddate,"000000000000");
		try {
			resultList=hBaseUtil.scanByRange(tableName, start_rowkey, stop_rowkey, family);
		} catch (IOException e) {
			
		}
		return resultList;
	}
	
	
	public  List<Map<String, String>> queryCommodityByDate(String dateRange,String commodityId,String tableName,String family){
		
		String startDate=dateRange.split(" - ")[0];
		String endDate=dateRange.split(" - ")[1];
		SimpleDateFormat sdf=new SimpleDateFormat("MM/dd/yyyy");
		List<Map<String, String>> resultList=new ArrayList<Map<String, String>>();
		try {
			Date start=sdf.parse(startDate);
			Date end=sdf.parse(endDate);
			String start_rowkey=generateRowkey(commodityId,start,"000000000");
			String end_rowkey=generateRowkey(commodityId,end,"999999999");
			resultList=hBaseUtil.scanByRange(tableName, start_rowkey, end_rowkey, family);
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return resultList;
	}

}
